package clusteringAlg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Size;

public class Main {

	public static void main(String[] args) {
		
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		
		int counter = 0;
		List<Person> personsPIE = new ArrayList<Person>();
		List<Person> personsEURECOM = new ArrayList<Person>();
		Help h = new Help(new Size(64,64));
		File folder;
		SampleSelectorWorker ssw = new SampleSelectorWorker();
		String path;
		
		folder = new File("D:/skola/diplomovka/Automaticky_vyber/KODY_VOJTO/PIE_faces_notpreprocessed_only");
	    h.listFilesForFolder(folder, personsPIE, FaceDB.PIE);
	    
	    System.out.println("PIE nacitane a ulozene");
    
	    folder = new File("D:/skola/diplomovka/EURECOM_KINECT_Photos");
	    h.listFilesForFolder(folder, personsEURECOM, FaceDB.EURECOM);
	    
	    System.out.println("EURECOM nacitane a ulozene");
	    
	    for (int i=2; i<8; i++){
	    	counter = 0;
		    for (Person p: personsPIE){
		    	path = "D:/skola/diplomovka/vysledkyPIE/Clustre_" + i + "/p_" + counter++;
		    	RunTest(p, FaceDB.PIE, path, i, 500, 2);
		    }
	    }
	    
	    for (int i=2; i<8; i++){
	    	counter = 0;
		    for (Person p: personsEURECOM){
		    	path = "D:/skola/diplomovka/vysledkyEURECOM/Clustre_" + i + "/p_" + counter++;
		    	RunTest(p, FaceDB.EURECOM, path, i, 500, 2);
		    }
	    }
	    
	    /*path = "D:/skola/diplomovka/vysledky/p38_DB";		
	    RunTest(personsPIE.get(38), FaceDB.PIE, path, 2, 500, 2);
	    
	    /*path = "D:/skola/diplomovka/vysledky/p10";
	    RunTest(personsPIE.get(0), FaceDB.PIE, path);*/
	    
	    /*path = "D:/skola/diplomovka/vysledky/test";
	    RunTest(personsEURECOM.get(0), FaceDB.EURECOM, path, 6, 500, 2, 9);*/
	    
	    /*ssw.CreateDBScan(personsEURECOM.get(0), 6, 2);
	    ssw.getSelector().Run();
	    
	    path = "D:/skola/diplomovka/vysledky/test";
	    h.saveImages(path, 0 ,  ssw.getSelector().GetMeanSamples());
		h.saveImages(path, 1 , ssw.getSelector().GetClosestSamples());
		h.savePoseIntoFile(path, ssw.getSelector().GetClosestSamples());*/
		
		System.out.println("done");
	}
	
	public static void RunTest(Person p, FaceDB fdb, String path, int nClusters, int iterations, int minPts){
		
		Help h = new Help(new Size(64,64));
		SampleSelectorWorker selector = new SampleSelectorWorker();
		String str = path;
		long startTime=0;
		
		selector.setDb(fdb);
		
		for (ProcessImage pi:ProcessImage.values()){
			if (pi != ProcessImage.UNDEFINED){
				//for (Algorithms alg: Algorithms.values()){
				Algorithms alg = Algorithms.DBScan;
				
					if (alg == Algorithms.DBScan && pi != ProcessImage.GAUSS_BLUR)
						continue;
				
					switch(alg){
					case Cmeans:
						selector.CreateCmeans(p, nClusters, iterations, pi);
						break;
					case DBScan:
						selector.CreateDBScan(p, nClusters, minPts); 
						break;
					case Kmeans:
						selector.CreateKmeans(p, nClusters, iterations, pi);
						break;
					case SOM:
						selector.CreateSOM(p, nClusters, iterations, pi);
						break;
					default:
						break;
					}
					
					str = selector.CreatePath(path);
					
					if(h.fileCount(str) == nClusters*2)
						return;
				
					startTime = System.nanoTime();
					selector.getSelector().Run();
					selector.setDurationTime(System.nanoTime()-startTime);
					
					System.out.println("alg is done");
					
					str = selector.CreatePath(path);
					
					h.eraseDirContent(str);
				    h.saveImages(str, 0 ,  selector.getSelector().GetMeanSamples());
					h.saveImages(str, 1 , selector.getSelector().GetClosestSamples());
					h.log("D:/skola/diplomovka/log", selector);
					//h.savePoseIntoFile(str, selector.getSelector().GetClosestSamples());
				
				//}	
			}
		}
	}
}
